
<template>
    <div>
    <template v-if="showPage">
       <component :is="currentView" :config="modelConfig" :formConfig="formConfig"  :relationInfo="relationInfo" :modelId="modelId" :isDetail="isDetail"
      v-show="showPage"  ref="listTable"/>
    </template>
    <template v-else>
        <div class="JNPF-common-title" v-if="formConfig&&formConfig.showTitle && formConfig.label">
          <h2>{{ formConfig.label }}</h2>
        </div>
        <p class="JNPF-Text" v-if="configOK">未关联到外键的数据，列表不可见</p>
        <p class="JNPF-Text" v-else>请选择关联的子表表单</p>
    </template>
    </div>
  </template>
  
  <script>
  import { getConfigData } from '@/api/onlineDev/visualDev'
  import Form from './form'
  import List from './list'
  import { message } from '@/utils/message'
  
  export default {
    name: 'list-table',
    components: { List,Form },
    props: {
        config: {
            type: Object,
            default: () => { }
        },
        value: {
            type: Array,
            default: () => ([])
        },
        foreignId: { //外键id
            type: String,
            default: ''
        },
        isDetail:{
            type: Boolean,
            default: false
        },
        formData: Object,
        disabled: {
            type: Boolean,
            default: false
        }
    },
    data() {
      return {
        currentView: '',
        showPage: false,
        configOK:false,//
        modelId: '',
        relationInfo:{
            fieldMap:this.config?this.config.fieldMap:{},
            foreignField:'',//外键字段
            foreignId:this.foreignId,//外键id
            
        },
        formConfig:this.config,
        modelConfig: {}
      }
    },
    created() {
        debugger
        if(!this.config){
            return
        }
      this.modelId = this.config.modelId
      if (!this.modelId) return
      
      this.relationInfo.foreignField = this.config.foreignField
      this.configOK = this.config.modelId&&this.relationInfo.foreignField?true:false
      if(!this.relationInfo.foreignId||!this.relationInfo.foreignField){
        return
      }
      this.getConfigData()
    },
    methods: {
      getConfigData() {
        getConfigData(this.modelId).then(res => {
          if (res.code !== 200) {
            console.error(res.msg)
            return
          }
          if (!res.data) return
          this.modelConfig = res.data
          if (res.data.webType == '1') {
            // console.error('子表单设置为列表类型')
            // return
            this.currentView = 'Form'
          } else {
            this.currentView = 'List'
          }
          this.showPage = true
        }).catch(() => { })
      }
    }
  }
  </script>
  